{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 手写体分类\n",
    "## 操作系统:windows 10 pro\n",
    "## Python3.6\n",
    "## Tensorflow-gpu==1.12.0 for cuda9.0 cudnn7\n",
    "## keras==2.2.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n",
      "c:\\users\\zhaoxingjie\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\tensorflow\\python\\framework\\dtypes.py:523: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_qint8 = np.dtype([(\"qint8\", np.int8, 1)])\n",
      "c:\\users\\zhaoxingjie\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\tensorflow\\python\\framework\\dtypes.py:524: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_quint8 = np.dtype([(\"quint8\", np.uint8, 1)])\n",
      "c:\\users\\zhaoxingjie\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\tensorflow\\python\\framework\\dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_qint16 = np.dtype([(\"qint16\", np.int16, 1)])\n",
      "c:\\users\\zhaoxingjie\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\tensorflow\\python\\framework\\dtypes.py:526: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_quint16 = np.dtype([(\"quint16\", np.uint16, 1)])\n",
      "c:\\users\\zhaoxingjie\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\tensorflow\\python\\framework\\dtypes.py:527: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_qint32 = np.dtype([(\"qint32\", np.int32, 1)])\n",
      "c:\\users\\zhaoxingjie\\appdata\\local\\programs\\python\\python36\\lib\\site-packages\\tensorflow\\python\\framework\\dtypes.py:532: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  np_resource = np.dtype([(\"resource\", np.ubyte, 1)])\n"
     ]
    }
   ],
   "source": [
    "import keras\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from keras.datasets import mnist as mnist\n",
    "from keras import layers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "(train_image, train_label), (test_image, test_label) = mnist.load_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x23cdd41a940>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAPsAAAD4CAYAAAAq5pAIAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4xLjMsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+AADFEAAAOxUlEQVR4nO3df6zddX3H8derP2ilBdJaKA10tkCNdBqrXqixymBExCYMDGraZKRzbDWxbJrVZYRlypYtI0w0DJRZpbP+wpggaxWiYCN26Ox6IaU/LNAWCpTWXqE4Ctr23t73/rin5kLv93Nuz/d7zvfQz/OR3Jxzvu/z/X7fHO6r33PP53u+H0eEAJz4xt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kTEkxFxWNJ3JF1ZQx9dLyLWSdr/msVXSlrVuL9KQ78sHVfQW1eIiL0R8Ujj/gFJR6cZr/W1S/TVEXWE/SxJzw57vFvdNd97SLrf9sO2l9bdzAimR8ReaeiXR9IZNffzWk2n8e6k10wz3jWvXSvTn5dVR9hHmkqqm8b/FkTEOyV9UNKyxttVjM6opvHulBGmGe8KrU5/XlYdYd8taeawx2dL2lNDHyOKiD2N2z5J96j7pqLed3QG3cZtX839/F43TeM90jTj6oLXrs7pz+sI+wZJc2zPtn2SpEWS1tTQxzFsT2p8cCLbkyRdpu6binqNpCWN+0skra6xl1fplmm8i6YZV82vXe3Tn0dEx38kLdTQJ/I7Jf19HT0U9HWOpEcbP1vr7k3SXRp6W9evoXdE10p6o6S1krY3bqd2UW/fkLRZ0iYNBWtGTb29V0N/Gm6StLHxs7Du1y7RV0deN06XBTLBGXRAJgg7kAnCDmSCsAOZIOxAJgg7kAnCDmTi/wHD/WHHoDzlRwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.imshow(train_image[12])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train_label[12]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = keras.Sequential()\n",
    "model.add(layers.Flatten())\n",
    "model.add(layers.Dense(64, activation='relu'))\n",
    "# model.add(layers.Dense(32, activation='relu'))\n",
    "model.add(layers.Dense(10, activation='softmax'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.compile(optimizer='sgd',\n",
    "              loss='sparse_categorical_crossentropy',\n",
    "              metrics=['acc']\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch 1/200\n",
      "60000/60000 [==============================] - 2s 37us/step - loss: 11.3479 - acc: 0.2940\n",
      "Epoch 2/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 7.6198 - acc: 0.5247\n",
      "Epoch 3/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 6.0459 - acc: 0.6231\n",
      "Epoch 4/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.8340 - acc: 0.6985\n",
      "Epoch 5/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 4.7029 - acc: 0.7071\n",
      "Epoch 6/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.5394 - acc: 0.7175\n",
      "Epoch 7/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.5584 - acc: 0.7164\n",
      "Epoch 8/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.6009 - acc: 0.7139\n",
      "Epoch 9/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.4453 - acc: 0.7234\n",
      "Epoch 10/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.3987 - acc: 0.7265\n",
      "Epoch 11/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.4111 - acc: 0.7257\n",
      "Epoch 12/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.3169 - acc: 0.7317\n",
      "Epoch 13/200\n",
      "60000/60000 [==============================] - 2s 42us/step - loss: 4.3984 - acc: 0.7266\n",
      "Epoch 14/200\n",
      "60000/60000 [==============================] - 2s 41us/step - loss: 4.4372 - acc: 0.7242\n",
      "Epoch 15/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.4409 - acc: 0.7240\n",
      "Epoch 16/200\n",
      "60000/60000 [==============================] - 2s 34us/step - loss: 4.3781 - acc: 0.7280\n",
      "Epoch 17/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.2939 - acc: 0.7330\n",
      "Epoch 18/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.2622 - acc: 0.7352\n",
      "Epoch 19/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.2430 - acc: 0.7364\n",
      "Epoch 20/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.1489 - acc: 0.7422\n",
      "Epoch 21/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.3033 - acc: 0.7326\n",
      "Epoch 22/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.1277 - acc: 0.7437\n",
      "Epoch 23/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 4.2102 - acc: 0.7383\n",
      "Epoch 24/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.2220 - acc: 0.7377\n",
      "Epoch 25/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.1402 - acc: 0.7429\n",
      "Epoch 26/200\n",
      "60000/60000 [==============================] - 2s 34us/step - loss: 4.1796 - acc: 0.7404\n",
      "Epoch 27/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.1927 - acc: 0.7396\n",
      "Epoch 28/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.2032 - acc: 0.7389\n",
      "Epoch 29/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.1507 - acc: 0.7423\n",
      "Epoch 30/200\n",
      "60000/60000 [==============================] - 2s 34us/step - loss: 4.1011 - acc: 0.7453\n",
      "Epoch 31/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.0817 - acc: 0.7465\n",
      "Epoch 32/200\n",
      "60000/60000 [==============================] - 2s 34us/step - loss: 4.1250 - acc: 0.7439\n",
      "Epoch 33/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.1223 - acc: 0.7439\n",
      "Epoch 34/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.1956 - acc: 0.7394\n",
      "Epoch 35/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.2089 - acc: 0.7386\n",
      "Epoch 36/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.1392 - acc: 0.7429\n",
      "Epoch 37/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 4.0510 - acc: 0.7484\n",
      "Epoch 38/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.1489 - acc: 0.7424\n",
      "Epoch 39/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.0670 - acc: 0.7475\n",
      "Epoch 40/200\n",
      "60000/60000 [==============================] - 2s 37us/step - loss: 4.0761 - acc: 0.7469\n",
      "Epoch 41/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 3.9978 - acc: 0.7518\n",
      "Epoch 42/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.0831 - acc: 0.7464\n",
      "Epoch 43/200\n",
      "60000/60000 [==============================] - 2s 36us/step - loss: 4.0021 - acc: 0.7516\n",
      "Epoch 44/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.0854 - acc: 0.7463\n",
      "Epoch 45/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.0632 - acc: 0.7478\n",
      "Epoch 46/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.0542 - acc: 0.7483\n",
      "Epoch 47/200\n",
      "60000/60000 [==============================] - 2s 34us/step - loss: 3.9663 - acc: 0.7537\n",
      "Epoch 48/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.0088 - acc: 0.7510\n",
      "Epoch 49/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.0789 - acc: 0.7468\n",
      "Epoch 50/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.1621 - acc: 0.7416\n",
      "Epoch 51/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.1100 - acc: 0.7448\n",
      "Epoch 52/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.0062 - acc: 0.7513\n",
      "Epoch 53/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 3.9983 - acc: 0.7518\n",
      "Epoch 54/200\n",
      "60000/60000 [==============================] - 2s 34us/step - loss: 4.1550 - acc: 0.7420\n",
      "Epoch 55/200\n",
      "60000/60000 [==============================] - 2s 35us/step - loss: 3.9853 - acc: 0.7526\n",
      "Epoch 56/200\n",
      "60000/60000 [==============================] - 2s 34us/step - loss: 4.0713 - acc: 0.7473\n",
      "Epoch 57/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.0089 - acc: 0.7511\n",
      "Epoch 58/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.0097 - acc: 0.7511\n",
      "Epoch 59/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.0712 - acc: 0.7473\n",
      "Epoch 60/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 4.0543 - acc: 0.7484\n",
      "Epoch 61/200\n",
      "60000/60000 [==============================] - 2s 34us/step - loss: 4.2675 - acc: 0.7351\n",
      "Epoch 62/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.0159 - acc: 0.7507\n",
      "Epoch 63/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 3.9707 - acc: 0.7535\n",
      "Epoch 64/200\n",
      "60000/60000 [==============================] - 2s 34us/step - loss: 4.0788 - acc: 0.7469\n",
      "Epoch 65/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.0911 - acc: 0.7461\n",
      "Epoch 66/200\n",
      "60000/60000 [==============================] - 2s 29us/step - loss: 4.0017 - acc: 0.7516\n",
      "Epoch 67/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.0314 - acc: 0.7498\n",
      "Epoch 68/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 4.0885 - acc: 0.7461\n",
      "Epoch 69/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 3.9589 - acc: 0.7543\n",
      "Epoch 70/200\n",
      "60000/60000 [==============================] - 2s 30us/step - loss: 3.9656 - acc: 0.7538\n",
      "Epoch 71/200\n",
      "60000/60000 [==============================] - 2s 30us/step - loss: 4.2016 - acc: 0.7391\n",
      "Epoch 72/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 4.0702 - acc: 0.7473\n",
      "Epoch 73/200\n",
      "60000/60000 [==============================] - 2s 30us/step - loss: 4.0683 - acc: 0.7475\n",
      "Epoch 74/200\n",
      "60000/60000 [==============================] - 2s 30us/step - loss: 3.9857 - acc: 0.7526\n",
      "Epoch 75/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 3.9578 - acc: 0.7543\n",
      "Epoch 76/200\n",
      "60000/60000 [==============================] - 2s 36us/step - loss: 3.9825 - acc: 0.7528\n",
      "Epoch 77/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 4.0428 - acc: 0.7490\n",
      "Epoch 78/200\n",
      "60000/60000 [==============================] - 2s 30us/step - loss: 4.0164 - acc: 0.7507\n",
      "Epoch 79/200\n",
      "60000/60000 [==============================] - 2s 30us/step - loss: 4.0185 - acc: 0.7506\n",
      "Epoch 80/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 4.1165 - acc: 0.7445\n",
      "Epoch 81/200\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "60000/60000 [==============================] - 2s 32us/step - loss: 3.9914 - acc: 0.7522\n",
      "Epoch 82/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 3.9893 - acc: 0.7524\n",
      "Epoch 83/200\n",
      "60000/60000 [==============================] - 2s 35us/step - loss: 4.0307 - acc: 0.7498\n",
      "Epoch 84/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.0070 - acc: 0.7513\n",
      "Epoch 85/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.0118 - acc: 0.7510\n",
      "Epoch 86/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 3.8969 - acc: 0.7581\n",
      "Epoch 87/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 3.9179 - acc: 0.7569\n",
      "Epoch 88/200\n",
      "60000/60000 [==============================] - 2s 34us/step - loss: 3.9945 - acc: 0.7521\n",
      "Epoch 89/200\n",
      "60000/60000 [==============================] - 2s 38us/step - loss: 4.1041 - acc: 0.7452\n",
      "Epoch 90/200\n",
      "60000/60000 [==============================] - 2s 30us/step - loss: 3.9111 - acc: 0.7573\n",
      "Epoch 91/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 3.9510 - acc: 0.7548\n",
      "Epoch 92/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 4.1012 - acc: 0.7455\n",
      "Epoch 93/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.1707 - acc: 0.7412\n",
      "Epoch 94/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 3.9800 - acc: 0.7530\n",
      "Epoch 95/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 3.9893 - acc: 0.7524\n",
      "Epoch 96/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 4.0675 - acc: 0.7475\n",
      "Epoch 97/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 3.9646 - acc: 0.7540\n",
      "Epoch 98/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.0181 - acc: 0.7506\n",
      "Epoch 99/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.0461 - acc: 0.7489\n",
      "Epoch 100/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.0138 - acc: 0.7509\n",
      "Epoch 101/200\n",
      "60000/60000 [==============================] - 2s 30us/step - loss: 4.0875 - acc: 0.7463\n",
      "Epoch 102/200\n",
      "60000/60000 [==============================] - 2s 30us/step - loss: 3.9645 - acc: 0.7539\n",
      "Epoch 103/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 3.9061 - acc: 0.7576\n",
      "Epoch 104/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.0105 - acc: 0.7511\n",
      "Epoch 105/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 3.9870 - acc: 0.7525\n",
      "Epoch 106/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.0601 - acc: 0.7481\n",
      "Epoch 107/200\n",
      "60000/60000 [==============================] - 2s 35us/step - loss: 3.9366 - acc: 0.7557\n",
      "Epoch 108/200\n",
      "60000/60000 [==============================] - 2s 34us/step - loss: 3.9712 - acc: 0.7536\n",
      "Epoch 109/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 3.9022 - acc: 0.7578\n",
      "Epoch 110/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.0650 - acc: 0.7477\n",
      "Epoch 111/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.0467 - acc: 0.7488\n",
      "Epoch 112/200\n",
      "60000/60000 [==============================] - 2s 35us/step - loss: 4.0774 - acc: 0.7469\n",
      "Epoch 113/200\n",
      "60000/60000 [==============================] - 2s 34us/step - loss: 4.0225 - acc: 0.7503\n",
      "Epoch 114/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.0141 - acc: 0.7509\n",
      "Epoch 115/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 3.9579 - acc: 0.7543\n",
      "Epoch 116/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 3.9574 - acc: 0.7544\n",
      "Epoch 117/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 3.8825 - acc: 0.7590\n",
      "Epoch 118/200\n",
      "60000/60000 [==============================] - 2s 30us/step - loss: 4.0319 - acc: 0.7498\n",
      "Epoch 119/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.1866 - acc: 0.7402\n",
      "Epoch 120/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 4.1815 - acc: 0.7405\n",
      "Epoch 121/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.0189 - acc: 0.7506\n",
      "Epoch 122/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 3.8526 - acc: 0.7609\n",
      "Epoch 123/200\n",
      "60000/60000 [==============================] - 2s 34us/step - loss: 3.9818 - acc: 0.7529\n",
      "Epoch 124/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.0266 - acc: 0.7501\n",
      "Epoch 125/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.1513 - acc: 0.7423\n",
      "Epoch 126/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 4.0840 - acc: 0.7465\n",
      "Epoch 127/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 4.0582 - acc: 0.7481\n",
      "Epoch 128/200\n",
      "60000/60000 [==============================] - 2s 34us/step - loss: 3.9233 - acc: 0.7565\n",
      "Epoch 129/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 3.9573 - acc: 0.7544\n",
      "Epoch 130/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 3.8677 - acc: 0.7600\n",
      "Epoch 131/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 3.9604 - acc: 0.7542\n",
      "Epoch 132/200\n",
      "60000/60000 [==============================] - 2s 34us/step - loss: 4.0474 - acc: 0.7489\n",
      "Epoch 133/200\n",
      "60000/60000 [==============================] - 2s 40us/step - loss: 4.1131 - acc: 0.7447\n",
      "Epoch 134/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 3.9690 - acc: 0.7537\n",
      "Epoch 135/200\n",
      "60000/60000 [==============================] - 2s 39us/step - loss: 3.9160 - acc: 0.7570\n",
      "Epoch 136/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 3.9619 - acc: 0.7541\n",
      "Epoch 137/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 3.8558 - acc: 0.7607\n",
      "Epoch 138/200\n",
      "60000/60000 [==============================] - 2s 38us/step - loss: 4.0045 - acc: 0.7514\n",
      "Epoch 139/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.0068 - acc: 0.7513\n",
      "Epoch 140/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 3.8879 - acc: 0.7587\n",
      "Epoch 141/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 3.9865 - acc: 0.7526\n",
      "Epoch 142/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 4.0070 - acc: 0.7513\n",
      "Epoch 143/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.1199 - acc: 0.7444\n",
      "Epoch 144/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.0072 - acc: 0.7513\n",
      "Epoch 145/200\n",
      "60000/60000 [==============================] - 2s 36us/step - loss: 4.0228 - acc: 0.7503\n",
      "Epoch 146/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.1344 - acc: 0.7435\n",
      "Epoch 147/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.0610 - acc: 0.7480\n",
      "Epoch 148/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.2416 - acc: 0.7368\n",
      "Epoch 149/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.2576 - acc: 0.7358\n",
      "Epoch 150/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.1927 - acc: 0.7398\n",
      "Epoch 151/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.0853 - acc: 0.7465\n",
      "Epoch 152/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.1983 - acc: 0.7394\n",
      "Epoch 153/200\n",
      "60000/60000 [==============================] - 2s 29us/step - loss: 3.9893 - acc: 0.7524\n",
      "Epoch 154/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 3.9451 - acc: 0.7552\n",
      "Epoch 155/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 3.8835 - acc: 0.7590\n",
      "Epoch 156/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 3.9367 - acc: 0.7557\n",
      "Epoch 157/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.1298 - acc: 0.7436\n",
      "Epoch 158/200\n",
      "60000/60000 [==============================] - 2s 34us/step - loss: 4.1916 - acc: 0.7398\n",
      "Epoch 159/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 4.1423 - acc: 0.7430\n",
      "Epoch 160/200\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "60000/60000 [==============================] - 2s 34us/step - loss: 4.0352 - acc: 0.7496\n",
      "Epoch 161/200\n",
      "60000/60000 [==============================] - 2s 30us/step - loss: 3.9972 - acc: 0.7519\n",
      "Epoch 162/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 3.9201 - acc: 0.7568\n",
      "Epoch 163/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 3.8876 - acc: 0.7587\n",
      "Epoch 164/200\n",
      "60000/60000 [==============================] - 2s 36us/step - loss: 3.9011 - acc: 0.7579\n",
      "Epoch 165/200\n",
      "60000/60000 [==============================] - 2s 30us/step - loss: 3.9504 - acc: 0.7549\n",
      "Epoch 166/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 3.9800 - acc: 0.7531\n",
      "Epoch 167/200\n",
      "60000/60000 [==============================] - 2s 29us/step - loss: 3.9215 - acc: 0.7567\n",
      "Epoch 168/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 3.9384 - acc: 0.7556\n",
      "Epoch 169/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 3.9823 - acc: 0.7529\n",
      "Epoch 170/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 3.8670 - acc: 0.7600\n",
      "Epoch 171/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 4.1978 - acc: 0.7395\n",
      "Epoch 172/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.1683 - acc: 0.7413\n",
      "Epoch 173/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 3.9886 - acc: 0.7525\n",
      "Epoch 174/200\n",
      "60000/60000 [==============================] - 2s 30us/step - loss: 3.9205 - acc: 0.7567\n",
      "Epoch 175/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 3.9507 - acc: 0.7548\n",
      "Epoch 176/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.2469 - acc: 0.7365\n",
      "Epoch 177/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 3.9345 - acc: 0.7558\n",
      "Epoch 178/200\n",
      "60000/60000 [==============================] - 2s 30us/step - loss: 3.8658 - acc: 0.7601\n",
      "Epoch 179/200\n",
      "60000/60000 [==============================] - 2s 29us/step - loss: 3.8344 - acc: 0.7621\n",
      "Epoch 180/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 3.8637 - acc: 0.7603\n",
      "Epoch 181/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 3.9375 - acc: 0.7556\n",
      "Epoch 182/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 3.9467 - acc: 0.7551\n",
      "Epoch 183/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 3.8764 - acc: 0.7595\n",
      "Epoch 184/200\n",
      "60000/60000 [==============================] - ETA: 0s - loss: 4.0319 - acc: 0.749 - 2s 31us/step - loss: 4.0373 - acc: 0.7495\n",
      "Epoch 185/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 4.0904 - acc: 0.7462\n",
      "Epoch 186/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 3.9323 - acc: 0.7560\n",
      "Epoch 187/200\n",
      "60000/60000 [==============================] - 2s 30us/step - loss: 4.0480 - acc: 0.7488\n",
      "Epoch 188/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.0125 - acc: 0.7510\n",
      "Epoch 189/200\n",
      "60000/60000 [==============================] - 2s 30us/step - loss: 3.9338 - acc: 0.7559\n",
      "Epoch 190/200\n",
      "60000/60000 [==============================] - 2s 30us/step - loss: 3.9142 - acc: 0.7571\n",
      "Epoch 191/200\n",
      "60000/60000 [==============================] - 2s 29us/step - loss: 3.9030 - acc: 0.7578\n",
      "Epoch 192/200\n",
      "60000/60000 [==============================] - 2s 32us/step - loss: 4.0181 - acc: 0.7506\n",
      "Epoch 193/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 3.9667 - acc: 0.7539\n",
      "Epoch 194/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 3.8846 - acc: 0.7589\n",
      "Epoch 195/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 4.0238 - acc: 0.7503\n",
      "Epoch 196/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 3.9702 - acc: 0.7536\n",
      "Epoch 197/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 3.9528 - acc: 0.7547\n",
      "Epoch 198/200\n",
      "60000/60000 [==============================] - 2s 33us/step - loss: 4.0903 - acc: 0.7462\n",
      "Epoch 199/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 3.9786 - acc: 0.7531\n",
      "Epoch 200/200\n",
      "60000/60000 [==============================] - 2s 31us/step - loss: 4.0080 - acc: 0.7513\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x23cf0a31c88>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(train_image, train_label, epochs=200, batch_size=128)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([3, 2, 1, 0, 4, 1, 4, 9, 5, 9], dtype=int64)"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argmax(model.predict(test_image[:10]), axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([7, 2, 1, 0, 4, 1, 4, 9, 5, 9], dtype=uint8)"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_label[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10000/10000 [==============================] - 1s 81us/step\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[4.0925892669677735, 0.746]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.evaluate(test_image, test_label)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "模型优化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = keras.Sequential()\n",
    "model.add(layers.Flatten())\n",
    "model.add(layers.Dense(64, activation='relu'))\n",
    "model.add(layers.Dense(64, activation='relu'))\n",
    "model.add(layers.Dense(64, activation='relu'))\n",
    "model.add(layers.Dense(64, activation='relu'))\n",
    "model.add(layers.Dense(64, activation='relu'))\n",
    "model.add(layers.Dense(64, activation='relu'))\n",
    "model.add(layers.Dense(10, activation='softmax'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.compile(optimizer='sgd',\n",
    "              loss='sparse_categorical_crossentropy',\n",
    "              metrics=['acc']\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Model: \"sequential_5\"\n",
      "_________________________________________________________________\n",
      "Layer (type)                 Output Shape              Param #   \n",
      "=================================================================\n",
      "flatten_5 (Flatten)          (None, 784)               0         \n",
      "_________________________________________________________________\n",
      "dense_17 (Dense)             (None, 64)                50240     \n",
      "_________________________________________________________________\n",
      "dense_18 (Dense)             (None, 64)                4160      \n",
      "_________________________________________________________________\n",
      "dense_19 (Dense)             (None, 64)                4160      \n",
      "_________________________________________________________________\n",
      "dense_20 (Dense)             (None, 64)                4160      \n",
      "_________________________________________________________________\n",
      "dense_21 (Dense)             (None, 64)                4160      \n",
      "_________________________________________________________________\n",
      "dense_22 (Dense)             (None, 64)                4160      \n",
      "_________________________________________________________________\n",
      "dense_23 (Dense)             (None, 10)                650       \n",
      "=================================================================\n",
      "Total params: 71,690\n",
      "Trainable params: 71,690\n",
      "Non-trainable params: 0\n",
      "_________________________________________________________________\n"
     ]
    }
   ],
   "source": [
    "model.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Train on 60000 samples, validate on 100 samples\n",
      "Epoch 1/20\n",
      "60000/60000 [==============================] - 3s 45us/step - loss: 4.8241e-05 - acc: 1.0000 - val_loss: 0.0060 - val_acc: 1.0000\n",
      "Epoch 2/20\n",
      "60000/60000 [==============================] - 3s 43us/step - loss: 4.7966e-05 - acc: 1.0000 - val_loss: 0.0056 - val_acc: 1.0000\n",
      "Epoch 3/20\n",
      "60000/60000 [==============================] - 3s 44us/step - loss: 4.7635e-05 - acc: 1.0000 - val_loss: 0.0057 - val_acc: 1.0000\n",
      "Epoch 4/20\n",
      "60000/60000 [==============================] - 3s 44us/step - loss: 4.7271e-05 - acc: 1.0000 - val_loss: 0.0055 - val_acc: 1.0000\n",
      "Epoch 5/20\n",
      "60000/60000 [==============================] - 3s 43us/step - loss: 4.6971e-05 - acc: 1.0000 - val_loss: 0.0057 - val_acc: 1.0000\n",
      "Epoch 6/20\n",
      "60000/60000 [==============================] - 3s 44us/step - loss: 4.6657e-05 - acc: 1.0000 - val_loss: 0.0063 - val_acc: 1.0000\n",
      "Epoch 7/20\n",
      "60000/60000 [==============================] - 3s 44us/step - loss: 4.6338e-05 - acc: 1.0000 - val_loss: 0.0059 - val_acc: 1.0000\n",
      "Epoch 8/20\n",
      "60000/60000 [==============================] - 3s 43us/step - loss: 4.6036e-05 - acc: 1.0000 - val_loss: 0.0060 - val_acc: 1.0000\n",
      "Epoch 9/20\n",
      "60000/60000 [==============================] - 3s 45us/step - loss: 4.5766e-05 - acc: 1.0000 - val_loss: 0.0060 - val_acc: 1.0000\n",
      "Epoch 10/20\n",
      "60000/60000 [==============================] - 3s 42us/step - loss: 4.5450e-05 - acc: 1.0000 - val_loss: 0.0053 - val_acc: 1.0000\n",
      "Epoch 11/20\n",
      "60000/60000 [==============================] - 3s 44us/step - loss: 4.5172e-05 - acc: 1.0000 - val_loss: 0.0057 - val_acc: 1.0000\n",
      "Epoch 12/20\n",
      "60000/60000 [==============================] - 3s 46us/step - loss: 4.4877e-05 - acc: 1.0000 - val_loss: 0.0059 - val_acc: 1.0000\n",
      "Epoch 13/20\n",
      "60000/60000 [==============================] - 3s 43us/step - loss: 4.4624e-05 - acc: 1.0000 - val_loss: 0.0057 - val_acc: 1.0000\n",
      "Epoch 14/20\n",
      "60000/60000 [==============================] - 3s 45us/step - loss: 4.4304e-05 - acc: 1.0000 - val_loss: 0.0060 - val_acc: 1.0000\n",
      "Epoch 15/20\n",
      "60000/60000 [==============================] - 3s 46us/step - loss: 4.4014e-05 - acc: 1.0000 - val_loss: 0.0059 - val_acc: 1.0000\n",
      "Epoch 16/20\n",
      "60000/60000 [==============================] - 3s 45us/step - loss: 4.3779e-05 - acc: 1.0000 - val_loss: 0.0059 - val_acc: 1.0000\n",
      "Epoch 17/20\n",
      "60000/60000 [==============================] - 3s 43us/step - loss: 4.3405e-05 - acc: 1.0000 - val_loss: 0.0058 - val_acc: 1.0000\n",
      "Epoch 18/20\n",
      "60000/60000 [==============================] - 3s 44us/step - loss: 4.3248e-05 - acc: 1.0000 - val_loss: 0.0054 - val_acc: 1.0000\n",
      "Epoch 19/20\n",
      "60000/60000 [==============================] - 3s 43us/step - loss: 4.2922e-05 - acc: 1.0000 - val_loss: 0.0061 - val_acc: 1.0000\n",
      "Epoch 20/20\n",
      "60000/60000 [==============================] - 3s 44us/step - loss: 4.2687e-05 - acc: 1.0000 - val_loss: 0.0055 - val_acc: 1.0000\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x23d276e8438>"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(train_image, train_label, epochs=20, batch_size=128, validation_data=(test_image[:100], test_label[:100]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10000/10000 [==============================] - 1s 116us/step\n"
     ]
    }
   ],
   "source": [
    "score = model.evaluate(test_image, test_label)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0.2939673472017454, 0.9657]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 8, 2, 0, 4, 9, 3, 5, 5, 1, 5, 6, 0, 3, 4, 4, 6, 5, 4, 6],\n",
       "      dtype=int64)"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.argmax(model.predict(test_image[145:165]), axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 8, 2, 0, 2, 9, 9, 5, 5, 1, 5, 6, 0, 3, 4, 4, 6, 5, 4, 6],\n",
       "      dtype=uint8)"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_label[145:165]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.save('mnist.h5')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Example\n",
    "\n",
    "```python\n",
    "from keras.models import load_model\n",
    "\n",
    "model.save('my_model.h5')  # creates a HDF5 file 'my_model.h5'\n",
    "del model  # deletes the existing model\n",
    "\n",
    "# returns a compiled model\n",
    "# identical to the previous one\n",
    "model = load_model('mnist.h5')\n",
    "```"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
